package com.wwl.springCloud.controller;

import cn.hutool.json.JSONUtil;
import com.wwl.springCloud.entity.Payment;
import com.wwl.springCloud.repository.PaymentRepository;
import com.wwl.springcloud.dto.CommonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.TimeUnit;

/**
 * @author wwl
 * @version 1.0
 * @date 2021/8/5 11:31
 * @description:
 */
@Api(value = "/payment",description = "支付", tags = {"payment"})
@RestController
@Slf4j
@RequestMapping("/payment")
public class PaymentJPAController {
    @Autowired
    private PaymentRepository paymentRepository;
    @Resource
    private DiscoveryClient discoveryClient;

    @Value("${server.port}")
    private String serverPort;
    private CommonResult commonResult = CommonResult.getInstance();
    /**
     * 新增或编辑
     */
    @ApiOperation("创建列表")
    @PostMapping("/save")
    public CommonResult save(@RequestBody Payment payment){
        Payment payment1 = paymentRepository.save(payment);
        if (null!=payment1){
            return new CommonResult("操作成功:"+serverPort+"!!",payment1);
        }
        return commonResult.error("添加失败!!");
    }

    /**
     * 删除
     */
    @ApiOperation("删除列表")
    @PostMapping("/delete")
    public CommonResult delete(int id){
        Optional<Payment> payment=paymentRepository.findById(id);
        if(payment.isPresent()){
            paymentRepository.deleteById(id);
            System.out.println("测试热部署！！");
            return commonResult.success("删除成功");
        }else{
            return commonResult.error("没有找到该对象");
        }
    }

    /**
     * 查询
     */
    @ApiOperation("查询列表")
    @PostMapping("/find")
    public CommonResult find(@RequestBody Map<String,Object> map){
        Integer id =Integer.valueOf(map.get("id").toString());
        Optional<Payment> payment=paymentRepository.findById(id);
        if(payment.isPresent()){
            return new CommonResult("操作成功:"+serverPort+"!!",payment);
        }else{
            return commonResult.error("没有找到该对象");
        }
    }

    @ApiOperation("查询列表")
    @PostMapping("/discovery")
    public CommonResult discovery(){
        List<String> services = discoveryClient.getServices();
        log.info("services:{}", JSONUtil.toJsonPrettyStr(services));

        List<ServiceInstance> instanceList = discoveryClient.getInstances("CLOUD-PAYMENT-SERVICE");
        log.info("instanceList:{}",JSONUtil.toJsonStr(instanceList));
        for (ServiceInstance serviceInstance:instanceList){
            log.info(serviceInstance.getServiceId()+"\t"+serviceInstance.getHost()+"\t"+serviceInstance.getPort()+"\t"+serviceInstance.getUri());
        }
        return  new CommonResult(this.discoveryClient);
    }

    @ApiOperation("查询列表")
    @GetMapping(value = "/payment/lb")
    public String getPaymentLB(){
        return serverPort;
    }

    @ApiOperation("超时服务")
    @GetMapping(value = "/feign/timeout")
    public String paymentFeignTimeout(){
        try { TimeUnit.SECONDS.sleep(3); }catch (Exception e) {e.printStackTrace();}
        return serverPort;
    }

    @GetMapping("/zipkin")
    public String paymentZipkin()
    {
        return "hi ,i'am paymentzipkin server fall back，welcome to atguigu，O(∩_∩)O哈哈~";
    }

}
